﻿if (typeof ($) != "undefined") {
    $(document).ready(function () {
        $("#divItem").html("");
        //instance[0].ItemId = '';
        var id = getPar1("id");
        if (id == "") {
            var date = new Date();
            $("#ValidDate").val(date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate());
            $("#ExpireDate").val("2030-12-31");
        }
        //获取初始化的薪资项目
        var url = "../Extend/AjaxData/jsonlist.aspx?entity=salSalaryItem&condition=SalarySolutionID is null or SalarySolutionID='00000000-0000-0000-0000-000000000000'&r=" + Math.random();
        $.get(url, function (d, s) {
            var list = eval("(" + d + ")"); //d.replace(/\\\\/g,'\\')
            var n = 0;
            if (list.total > 0) {
                for (var o in list.rows) {
                    var t = list.rows[o].ItemName;
                    var v = list.rows[o].ID;
                    var tt = list.rows[o].ItemNotes;
                    if (typeof (t) != "undefined") {
                        n++;
                        $("#divItem").after("<input id='chk" + n + "' name='chk' type='checkbox' title='" + t + "' value='" + v + "' />" + tt);
                    }
                }
            }
        });
        if (id != "") {
            //获取已有薪资项
            setTimeout(function () {
                url = "../Extend/AjaxData/jsonlist.aspx?entity=salSalaryItem&condition=SalarySolutionID='" + id + "'&r=" + Math.random();
                $.get(url, function (d, s) {
                    var list = eval("(" + d + ")");
                    if (list.total > 0) {
                        for (var o in list.rows) {
                            var t = list.rows[o].ItemName;
                            var v = list.rows[o].ID;
                            if (typeof (t) != "undefined") {
                                $("input[name=chk]").each(function () {
                                    if ($(this).attr("title") == t) {
                                        $(this).attr("checked", true);
                                    }
                                });
                            }
                        }
                    }
                });
            }, 200)
        }

        setTimeout(function () {
            $("input[name=chk]").click(function () {
                var itemIds = '';
                $("input[name=chk]").each(function () {
                    if (this.checked) {
                        itemIds += $(this).val() + ',';
                    }
                });
                instance[0].ItemId = itemIds;
            });
        }, 200);
    });
}

//数据验证
var fnCheckData = function () {
    if ($("#Name").val() == "" || $("#Name").val() == null) {
        return "方案名称不能为空！";
    }
    if (instance[0].ItemId == '' || instance[0].ItemId == null) {
        return "没有选择薪资项！";
    }
    if (($("#ValidDate").val() == "" || $("#ValidDate").val() == null)) {
        return "生效日期不能为空！";
    }
    if ($("#ExpireDate").val() == "" || $("#ExpireDate").val() == null) {
        return "失效日期不能为空！";
    }
}